using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum RedDotType
{
    /**
     * 只显示红点
     */
    Normal, 
    /**
     * 节点数字红点，子节点有几个红点，就显示几个数字
     */
    RedDotNodeNum, 
    /**
     *  红点数据个数，根据数据的个数，显示红点数量
     */
    RedDotDataNum,
}
public class RedDotSystem 
{
    private static RedDotSystem _instance;

    public static RedDotSystem Instance
    {
        get
        {
            if (_instance == null) _instance = new RedDotSystem();
            return _instance;
        }
    }

    /// <summary>
    /// 红点字典
    /// </summary>
    private Dictionary<RedDotDefine, RedDotTreeNode> mRedDotLogicDic = new Dictionary<RedDotDefine, RedDotTreeNode>();
    /// <summary>
    /// 初始化红点树
    /// </summary>
    /// <param name="nodelist"></param>
    public void InitlizateRedDotTree(List<RedDotTreeNode> nodelist)
    {
        foreach (var item in nodelist)
        {
            mRedDotLogicDic.Add(item.node, item);
        }
    }
    
    /// <summary>
    /// 更新红点状态
    /// </summary>
    public void UpdateRedDotState(RedDotDefine redKey)
    {
        //到树顶了
        if (redKey== RedDotDefine.None)
        {
            return;
        }
        RedDotTreeNode redDotNode = null;
        if (mRedDotLogicDic.TryGetValue(redKey, out redDotNode))
        {
            redDotNode.RefreshRedDotState();
            //通知父节点
            UpdateRedDotState(redDotNode.parentNode);
        }
    }
    
    /// <summary>
    /// 注册红点状态改变事件
    /// </summary>
    public void RegisterRedDotChangeEvent(RedDotDefine redKey, System.Action<RedDotType, bool, int> changeEvent)
    {
        RedDotTreeNode redDotNode = null;
        if (mRedDotLogicDic.TryGetValue(redKey, out redDotNode))
        {
            redDotNode.OnRedDotActiveChange += changeEvent;
        }
        else
        {
            Debug.LogError($"key:{redKey} not exits,plase check key define is error!");
        }
    }
    /// <summary>
    /// 移除红点状态改变事件监听
    /// </summary>
    public void UnRegisterRedDotChangeEvent(RedDotDefine redKey, System.Action<RedDotType, bool, int> changeEvent)
    {
        RedDotTreeNode redDotNode = null;
        if (mRedDotLogicDic.TryGetValue(redKey, out redDotNode))
        {
            redDotNode.OnRedDotActiveChange -= changeEvent;
        }
        else
        {
            Debug.LogError($"key:{redKey} not exits,plase check key define is error!");
        }
    }
    /// <summary>
    /// 获取子节点红点个数
    /// </summary>
    public int GetChildNodeRedDotCount(RedDotDefine redKey)
    {
        int childRedDotCount = 0;
        ComputeChildRedDotCount(redKey, ref childRedDotCount);
        return childRedDotCount;
    }
    
    /// <summary>
    /// 计算子节点红点个数
    /// </summary>
    private void ComputeChildRedDotCount(RedDotDefine redKey,ref int childRedDotCount)
    {
        foreach (var item in mRedDotLogicDic.Values)
        {
            
            if (item.parentNode==redKey)
            {
                item.RefreshRedDotState();
                if (item.redDotActive)
                {
                    childRedDotCount += item.redDotCount;
                    // //处理节点数量类型的嵌套树结构。跳过其所有子节点,读取总结点个数
                    // if (item.redDotType == RedDotType.RedDotNodeNum)
                    // {
                    //     continue;
                    // }
                    //处理红点数据个数类型的嵌套数结构。跳过其所有子节点,读取总结点个数
                    if (item.redDotType!= RedDotType.RedDotDataNum)
                    {
                        ComputeChildRedDotCount(item.node,ref childRedDotCount);
                    }
                }
            }
        }
    }
}